package com.xuezhy.mapdrawdemo.utils;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.xuezhy.mapdrawdemo.bean.DrawLatLng;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 高德计算工具类
 * @author: Adobe Chow
 * @date: 2018/6/7 14:36
 * @Copyright: www.winshang.com Inc. All rights reserved.
 */
public class GaodeUtils {


    /**
     * 计算经纬度围成的实际面积（平方公里）
     *
     * @return
     */
    public static double getArea(List<DrawLatLng> ring) {

        double sJ = 6378137;
        double Hq = 0.017453292519943295;
        double c = sJ * Hq;
        double d = 0;

        if (3 > ring.size()) {
            return 0;
        }

        for (int i = 0; i < ring.size() - 1; i++) {
            LatLng h = ring.get(i).getLatLng();
            LatLng k = ring.get(i + 1).getLatLng();
            double u = h.longitude * c * Math.cos(h.latitude * Hq);
            double hhh = h.latitude * c;
            double v = k.longitude * c * Math.cos(k.latitude * Hq);
            d = d + (u * k.latitude * c - v * hhh);
        }
        LatLng g1 = ring.get(ring.size() - 1).getLatLng();
        LatLng point = ring.get(0).getLatLng();
        double eee = g1.longitude * c * Math.cos(g1.latitude * Hq);
        double g2 = g1.latitude * c;
        double k = point.longitude * c * Math.cos(point.latitude * Hq);
        d += eee * point.latitude * c - k * g2;
        BigDecimal divide = new BigDecimal(0.5 * Math.abs(d)).divide(new BigDecimal(1000000));
        return divide.doubleValue() * 1000000;
    }
}